package com.tanhua.soul.controller;

import com.tanhua.soul.service.SoulService;
import com.tanhua.soul.vo.QuestionnaireVo;
import com.tanhua.soul.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: Shanhai
 * Never say Never
 * @Date: 2020/12/14 15:27
 */
@RestController
@RequestMapping("35")
public class SoulController {

    @Autowired
    private SoulService soulService;

    @GetMapping("testSoul")
    public List<QuestionnaireVo> queryQuestionnaireList (){
        return soulService.listQuestionnaire();
    }


    @PostMapping("testSoul")
    public String commitQuestionnaire(@RequestBody Map<String, List<Map<String, String>>> answers) {

        List<Map<String, String>> answers1 = answers.get("answers");

        List<Integer> questionIds = new ArrayList();
        List<Integer> optionIds = new ArrayList();

        for (Map<String, String> stringStringMap : answers1) {
            questionIds.add(Integer.parseInt(stringStringMap.get("questionId")));
            optionIds.add(Integer.parseInt(stringStringMap.get("optionId")));
        }
        return soulService.getReportId(questionIds, optionIds);
    }


    //TODO 因为传 中文 可能会报错，所以这里改成了 1-初级，2-中级，3-高级
    @GetMapping("/testSoul/report/{id}")
    public ResultVo soulTestResult(
            @PathVariable("id")String reportId){
        try {
            ResultVo resultVo = soulService.testResultService(reportId);
            return resultVo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
